﻿using DataBase.Core;
using System;

//----------------------------------------------------------------*/
// 版权所有：
// 文 件 名：DbFactory.cs
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2014/7/10 22:15:39
// 修改描述：
//----------------------------------------------------------------*/
namespace DataBase.Common
{
    internal class DbFactory
    {
        internal static IDbHelper _helper;
        private static object _locker = new Object();

        /// <summary>
        /// 获取实例
        /// </summary>
        /// <param name="conn">数据库链接</param>
        /// <param name="dbClass">数据库操作类</param>
        /// <returns></returns>
        private static IDbHelper GetHelper(string conn, SqlSourceType sourceType)
        {
            //if (helper != null && (conn + "").Length != 0 && !conn.Equals(DBConfig.ConnString))
            //{
            //    helper = null;
            //}
            if (String.IsNullOrEmpty(conn))
                conn = DBConfig.ConnString;

            switch (sourceType)
            {
                case SqlSourceType.SQLServer:
                    _helper = new SqlServerHelper(conn);
                    break;
                //case SqlSourceType.MySQL:
                //    _helper = new MySqlHelper(conn);
                //    break;
                //case SqlSourceType.Access:
                //    _helper = new OleDbHelper(conn);
                //    break;
            }
            return _helper;
        }
        /// <summary>
        /// 获取实例
        /// </summary>
        /// <param name="conn">数据库链接</param>
        /// <returns></returns>
        internal static IDbHelper GetHelper(string conn)
        {
            //数据库连接与默认的不同
            if (_helper != null && (conn + "").Length != 0)//&& !connString.Equals(DBConfig.ConnString))
            {
                _helper = null;
            }
            if (_helper == null)
            {
                lock (_locker)
                {
                    if (_helper == null)
                    {
                        if ((conn + "").Length == 0)
                        {
                            _helper = GetHelper(null, DBConfig.DatabaseType);
                        }
                        else
                        {
                            _helper = GetHelper(conn, DBConfig.DatabaseType);
                        }
                    }
                }
            }

            return _helper;
        }
        /// <summary>
        /// 获取实例
        /// </summary>
        /// <returns></returns>
        internal static IDbHelper GetHelper()
        {
            return GetHelper(DBConfig.ConnString, DBConfig.DatabaseType);
        }
    }
}
